package com.winning.client;

import com.winning.platwebservice.service.CommonService;
import org.apache.cxf.endpoint.Client;
import org.apache.cxf.jaxws.JaxWsProxyFactoryBean;
import org.apache.cxf.jaxws.endpoint.dynamic.JaxWsDynamicClientFactory;

public class CxfClient {
	public static void main(String[] args) {
		cl1();
	}

	/**
	 * 方式1.代理类工厂的方式,需要拿到对方的接口
	 */
	public static void cl1() {
		try {
			// 接口地址
			String address = "http://127.0.0.1:8088/PlatService/CommonService?wsdl";
			// 代理工厂
			JaxWsProxyFactoryBean jaxWsProxyFactoryBean = new JaxWsProxyFactoryBean();
			// 设置代理地址
			jaxWsProxyFactoryBean.setAddress(address);
			// 设置接口类型
			jaxWsProxyFactoryBean.setServiceClass(CommonService.class);
			// 创建一个代理接口实现
			CommonService cs = (CommonService) jaxWsProxyFactoryBean.create();
			// 数据准备

            System.out.println(cs);

//			String userName = "<Request><body><test><id>1</id>  <!--医疗机构代码--><name>0000000029111</name>  <!--就诊流水号--></test><test><id>3</id>  <!--医疗机构代码--><name>000000011</name>  <!--就诊流水号--></test></body></Request>";
            String sResponse =
                "<Request>\n" +
                        "<body>\n" +
                        "<A_SCCS>\n" +
                        "<NAME>SJ</NAME>  <!--医疗机构代码-->\n" +
                        "<CODE>SUJUN</CODE>  <!--就诊流水号-->\n" +
                        "<XM>苏君</XM>  <!--就诊流水号-->\n" +
                        "<XB>1</XB>  <!--就诊流水号-->\n" +
                        "<NL>2</NL>  <!--就诊流水号-->\n" +
                        "</A_SCCS>\n" +
                        "<A_SCCS>\n" +
                        "<NAME>ZDD</NAME>  <!--医疗机构代码-->\n" +
                        "<CODE>zhoudongdong</CODE>  <!--就诊流水号-->\n" +
                        "<XM>周冬冬</XM>  <!--就诊流水号-->\n" +
                        "<XB>1</XB>  <!--就诊流水号-->\n" +
                        "<NL>5</NL>  <!--就诊流水号-->\n" +
                        "</A_SCCS>\n" +
                        "</body>\n" +
                        "</Request>\n";

            // 调用代理接口的方法调用并返回结果
			String result = cs.DataService(sResponse);
			System.out.println("返回结果:" + result);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * 动态调用方式
	 */
	public static void cl2() {
		// 创建动态客户端
		JaxWsDynamicClientFactory dcf = JaxWsDynamicClientFactory.newInstance();
		Client client = dcf.createClient("http://localhost:8080/PlatService/CommonService?wsdl");
		// 需要密码的情况需要加上用户名和密码
		// client.getOutInterceptors().add(new ClientLoginInterceptor(USER_NAME,
		// PASS_WORD));
		Object[] objects = new Object[0];
		try {
			// invoke("方法名",参数1,参数2,参数3....);
			objects = client.invoke("DataService", "sj");
			System.out.println("返回数据:" + objects[0]);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}
